﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.ViewInfo;
using System.ComponentModel;
using System.Drawing;
using XCI.Extensions;

namespace XCI.Sys.Controls
{
    [System.ComponentModel.DesignerCategory("Code")]
    public class StatusTextEdit : DevExpress.XtraEditors.TextEdit
    {
        static StatusTextEdit()
        {
            RepositoryItemStatusTextEdit.RegisterStatusTextEdit();
        }

        public StatusTextEdit()
        {
        }

        /// <summary>
        /// True文本
        /// </summary>
        [DefaultValue("启用")]
        [Category("XCI")]
        [Description("True文本")]
        public virtual string TrueText { get; set; } = "启用";

        /// <summary>
        /// False文本
        /// </summary>
        [DefaultValue("禁用")]
        [Category("XCI")]
        [Description("False文本")]
        public virtual string FalseText { get; set; } = "禁用";

        /// <summary>
        /// 空文本
        /// </summary>
        [DefaultValue(null)]
        [Category("XCI")]
        [Description("空文本")]
        public virtual string NullText { get; set; }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override object EditValue
        {
            get
            {
                var r = base.EditValue.ToBoolOrNull();
                if (r == null)
                {
                    return NullText;
                }
                if (r.Value)
                {
                    base.ForeColor = Color.Green;
                    return TrueText;
                }
                base.ForeColor = Color.Red;
                return FalseText;
            }
            set => base.EditValue = value;
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemStatusTextEdit Properties => base.Properties as RepositoryItemStatusTextEdit;

        public override string EditorTypeName => RepositoryItemStatusTextEdit.CustomEditName;
    }

    [UserRepositoryItem("RegisterStatusTextEdit")]
    public class RepositoryItemStatusTextEdit : RepositoryItemTextEdit
    {
        static RepositoryItemStatusTextEdit()
        {
            RegisterStatusTextEdit();
        }

        public const string CustomEditName = "StatusTextEdit";

        public RepositoryItemStatusTextEdit()
        {
        }

        public override string EditorTypeName => CustomEditName;

        public static void RegisterStatusTextEdit()
        {
            Image img = null;
            EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(CustomEditName, typeof(StatusTextEdit), typeof(RepositoryItemStatusTextEdit), typeof(StatusTextEditViewInfo), new StatusTextEditPainter(), true, img));
        }

        public override void Assign(RepositoryItem item)
        {
            BeginUpdate();
            try
            {
                base.Assign(item);
                RepositoryItemStatusTextEdit source = item as RepositoryItemStatusTextEdit;
                if (source == null) return;
                //
            }
            finally
            {
                EndUpdate();
            }
        }
    }

    public class StatusTextEditViewInfo : TextEditViewInfo
    {
        public StatusTextEditViewInfo(RepositoryItem item) : base(item)
        {
        }
    }

    public class StatusTextEditPainter : TextEditPainter
    {
        public StatusTextEditPainter()
        {
        }
    }
}